
import QWEventDispatcher from "../GMDBase/QWEventDispatcher"
import QWEvent from "../GMDBase/QWEvent" 
import AccountEvts from "../GMDBase/AccountEvts";
import LCData from "../GMDWeakNetGameBase/LCData";
import GameConn from "../GMDWeakNetGameBase/GameConn";
import NSLoader from "../GMDWeakNetGameBase/NSLoader";
import FB from "./FB"; 
import { gcfg } from "../../gcfg";
export default class fbAccount
{ 
    public static addEventListener(type: string, listener: Function, thisObject: any): void{
        fbAccount.Events.addEventListener(type,listener,thisObject)
    }

    static Events:QWEventDispatcher = new QWEventDispatcher()

    //public static mode = "fb"
 

    // 启动登录过程
    public static login(obj:any) {  
      setTimeout(
          () => {
            var evt =  new QWEvent(AccountEvts.OnLoginWXSuccess) 
            fbAccount.Events.dispatchEvent(evt) 
          }, 
          100
      );
      
    }

    //生成NS登录包
    static GenerateNSLoginNM() {
        var nm = null
        if (!wx.IsQWDev) {
            nm = {
                n: 'ck',
                tp: 'fb',
                fb: {
                    a: gcfg["ProjectID"],
                    uid: FBInstant.player.getID(),
                    icon: FBInstant.player.getPhoto(),
                    name: FBInstant.player.getName()
                }
            }
        } else {
            console.log("FB平台，在研发模式无法登陆。")
        }

        return nm
    }

    public static  AutoUploadUserInfo(onSuccess:Function=null,onFail:Function=null)
    {
        if(!FB.InFB) return

        //保存用户头像和昵称
        LCData.Set(LCData.ParamName.NICK_NAME,FBInstant.player.getName()) 
        LCData.Set(LCData.ParamName.AVATOR_URL,FBInstant.player.getPhoto())  
        //检查
        if(
            LCData.IsEmpty(LCData.Get("UInfoOK") )//没有上传用户信息
        )
        {
            //提交用户数据
            var nm = {
                n:"UInfo",
                tp:"fb",
                fb:{ 
                    name:FBInstant.player.getName(),
                    icon:FBInstant.player.getPhoto()
                }
            }

            cc.log("开始上传用户信息")
            var loader = GameConn.Singletion.CreateLoader(nm)
            loader.addEventListener(
                NSLoader.OnComplete,
                (evt:QWEvent)=>{
                    var data = evt.details
                    if(data.r!=0) {
                        cc.log("上传用户基本信息遇到问题！")
                        return
                    }

                    cc.log("成功上传用户基本信息！") 

                    //上传成功标记
                    LCData.Set("UInfoOK","ok")

                    if(onSuccess) onSuccess()
                },
                this
            ) 

            loader.addEventListener(NSLoader.OnError,()=>{
                cc.log("上传用户基本信息异常！") 
                if(onFail) onFail() 
            })
        }else
        {
            if(onSuccess) onSuccess()
            cc.log("已经上传过用户信息了")
        }
    }
};
     